package com.baozhun.mall.common.base

import android.os.Bundle
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baozhun.mall.common.R
import com.baozhun.mall.common.databinding.ActivityBaseListBinding
import com.baozhun.mall.common.util.Constants
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.util.dp2px

/**
 *   列表基类
 *
 * @author xuefeng.zhu
 * @version 2.10.1
 * @since 2018/12/18/018
 */
abstract class BaseListActivity<VM : BaseViewModel, DB : ActivityBaseListBinding, ITEM_DB : ViewDataBinding, T : Any> :
    BaseActivity<VM, DB>(),
    OnRefreshListener, OnLoadMoreListener, OnItemChildClickListener, OnItemClickListener {

    private var mCurrentPage = 1 //当前页码

    /**
     * 基类列表
     */
    protected var mBaseListAdapter: BaseQuickAdapter<T, BaseDataBindingHolder<ITEM_DB>>? = null

    /**
     * 基类LayoutManager
     */
    private var mLayoutManager: RecyclerView.LayoutManager? = null

    /**
     * 是否为刷新，否则为上拉加载
     */
    private var mIsRefresh = false

    private var mListDatabind: ActivityBaseListBinding? = null

    protected abstract fun createAdapter(): BaseQuickAdapter<T, BaseDataBindingHolder<ITEM_DB>>?
    abstract fun loadData()
    abstract fun getTitleBarTitle(): Int
    protected fun getTitleBarLeftIcon(): Int = com.baozhun.mall.res.R.drawable.ic_titlebar_back
    protected fun createLayoutManage(): RecyclerView.LayoutManager? = LinearLayoutManager(this)
    protected open fun getItemDecoration(): RecyclerView.ItemDecoration? = null
    protected open fun getTopMargin(): Int = 0
    protected open fun isLoadEndMoreGone(): Boolean = false
    protected open fun enableRefresh(): Boolean = true

    override fun initDataBind(): View? {
        mListDatabind = DataBindingUtil.bind(View.inflate(this, R.layout.activity_base_list, null))
        mDataBinding = mListDatabind as DB
        return mDataBinding.root
    }

    override fun initView(savedInstanceState: Bundle?) {
        mListDatabind?.titleBar?.leftIcon = getDrawable(getTitleBarLeftIcon())
        mListDatabind?.titleBar?.title = getString(getTitleBarTitle())
        //初始化adapter
        mBaseListAdapter = createAdapter()
        mBaseListAdapter?.setOnItemChildClickListener(this)
        mBaseListAdapter?.setOnItemClickListener(this)
        mBaseListAdapter?.loadMoreModule?.setOnLoadMoreListener(this)
        mBaseListAdapter?.loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        //初始化recycleView
        mLayoutManager = createLayoutManage()
        mListDatabind?.baseRefreshLayout?.setOnRefreshListener(this)
        mListDatabind?.baseRefreshLayout?.setEnableLoadMore(enableRefresh())
        (mListDatabind?.baseRefreshLayout?.layoutParams as MarginLayoutParams).setMargins(0, dp2px(getTopMargin()), 0, 0)
        mListDatabind?.baseRecycleView?.layoutManager = mLayoutManager
        getItemDecoration()?.let { mListDatabind?.baseRecycleView?.addItemDecoration(it) }
        mListDatabind?.baseRecycleView?.adapter = mBaseListAdapter

    }


    override fun initData() {
        loadData()
    }

    /**
     * 刷新回调
     */
    override fun onRefresh(refreshLayout: RefreshLayout) {
        mIsRefresh = true
        mCurrentPage = 1
        loadData()
    }

    /**
     * 上拉加载更多
     */
    override fun onLoadMore() {
        mIsRefresh = false
        loadData()
    }

    /**
     * 设置数据
     *
     * @param objects 加载的数据
     */
    protected fun setListData(objects: MutableList<T>) {
        if (mIsRefresh) {
            onRefreshSuccess(objects)
        } else {
            onLoadMoreSuccess(objects)
        }
    }

    /**
     * 刷新成功
     *
     * @param objects 刷新加载的数据
     */
    private fun onRefreshSuccess(objects: MutableList<T>?) {
        mListDatabind?.baseRefreshLayout?.finishRefresh()
        mBaseListAdapter?.setList(objects)
    }

    /**
     * 分页成功
     *
     * @param objects 分页加载的数据
     */
    private fun onLoadMoreSuccess(objects: MutableList<T>) {
        mIsRefresh = false
        mBaseListAdapter?.addData(objects)
        mCurrentPage += 1
        if (objects.size < Constants.Common.PAGE_SIZE) {
            mBaseListAdapter?.loadMoreModule?.loadMoreEnd(isLoadEndMoreGone())
        } else {
            mBaseListAdapter?.loadMoreModule?.loadMoreComplete()
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
    }

}